<?php
if (!defined('IN_CONTEXT')) die('access violation error!');

class lib_mysql
{
	private $_database;
	private $_host;
	private $_user;
	private $_password;
	private $_prefix;
	private $_debug;
	public static $_obj;
	
	public static function init()
	{
		$current_class = __CLASS__;
		if(self::$_obj instanceof $current_class)
		{
			return self::$_obj;
		}
		else
		{
			self::$_obj = new $current_class();
			return self::$_obj;
		}
	}
	
	private function __construct()//singleton
	{
		$this->setInfo();
		$link = mysql_connect($this->_host,$this->_user,$this->_password);
		if(!$link) die("Can not connect mysql database.");
		mysql_select_db($this->_database);
		mysql_query("SET NAMES UTF8");
	}
	
	/**
	 * 
	 * return only one result
	 * @param string $table
	 * @param string $where 
	 * @param array $colum return these colum values e.g. array([0] => 'id',[1] => 'name')
	 * @return array
	 */
	public function find($table,$where = false,$colum = array())
	{
		try
		{
			$table = ($this->_prefix).$table; 
			if(empty($colum))
			{
				if(empty($where))
				{
					if($this->_debug == "true")
					{
						echo "SELECT DISTINCT * FROM $table";
					}
					$res = mysql_query("SELECT DISTINCT * FROM $table");
					if(!$res)
					{
						if($this->_debug == "true")
						{
							mysql_error();
							die;
						}
						else
						{
							die("Query database error.");
						}
					} 
					
					$result = mysql_fetch_array($res, MYSQL_ASSOC);
					return $result;
				}
				else
				{
					if($this->_debug == "true")
					{
						echo "SELECT DISTINCT * FROM $table WHERE $where";
					}
					$res = mysql_query("SELECT DISTINCT * FROM $table WHERE $where");
					if(!$res)
					{
						if($this->_debug == "true")
						{
							mysql_error();
							die;
						}
						else
						{
							die("Query database error.");
						}
					}
					
					$result = mysql_fetch_array($res, MYSQL_ASSOC);
					return $result;
				}
			}
			else
			{
				foreach($colum as $v)
				{
					$sql_str .= $v.',';
				}
				$sql_str = substr($sql_str, 0, strlen($sql_str) - 1);
				
				if(empty($where))
				{
					if($this->_debug == "true")
					{
						echo "SELECT DISTINCT $sql_str FROM $table";
					}
					$res = mysql_query("SELECT DISTINCT $sql_str FROM $table");
					if(!$res)
					{
						if($this->_debug == "true")
						{
							mysql_error();
							die;
						}
						else
						{
							die("Query database error.");
						}
					}
					
					$result = mysql_fetch_array($res, MYSQL_ASSOC);
					return $result;
				}
				else
				{
					if($this->_debug == "true")
					{
						echo "SELECT DISTINCT $sql_str FROM $table WHERE $where";
					}
					$res = mysql_query("SELECT DISTINCT $sql_str FROM $table WHERE $where");
					if(!$res)
					{
						if($this->_debug == "true")
						{
							mysql_error();
							die;
						}
						else
						{
							die("Query database error.");
						}
					}
					
					$result = mysql_fetch_array($res, MYSQL_ASSOC);
					return $result;
				}
			}
		}
		catch(Exception $ex)
		{
			if($this->_debug == "true")
			{
				echo $ex->getMessage();
				die;
			}
			else
			{
				echo "Can not query this table:$table";
				die;
			}
		}
	}
	
	/**
	 * @param string $table
	 * @param string $where 
	 * @param array $colum return these colum values e.g. array([0] => 'id',[1] => 'name')
	 * @return array Two-dimensional array
	 */
	public function findAll($table,$where = false,$colum = array())
	{
		$row = array();
		try
		{
			$table = ($this->_prefix).$table; 
			if(empty($colum))
			{
				if(empty($where))
				{
					if($this->_debug == "true")
					{
						echo "SELECT DISTINCT * FROM $table";
					}
					$res = mysql_query("SELECT DISTINCT * FROM $table");
					
					if(!$res)
					{
						if($this->_debug == "true")
						{
							mysql_error();
							die;
						}
						else
						{
							die("Query database error.");
						}
					} 
					
					while($result = mysql_fetch_array($res, MYSQL_ASSOC))
					{
						$row[] = $result;
					}
					return $row;
				}
				else
				{
					if($this->_debug == "true")
					{
						echo "SELECT DISTINCT * FROM $table WHERE $where";
					}
					$res = mysql_query("SELECT DISTINCT * FROM $table WHERE $where");
					
					if(!$res)
					{
						if($this->_debug == "true")
						{
							mysql_error();
							die;
						}
						else
						{
							die("Query database error.");
						}
					} 
					
					while($result = mysql_fetch_array($res, MYSQL_ASSOC))
					{
						$row[] = $result;
					}
					return $row;
				}
			}
			else
			{
				$sql_str = '';
				foreach($colum as $v)
				{
					$sql_str .= $v.',';
				}
				$sql_str = substr($sql_str, 0, strlen($sql_str) - 1);
				
				if(empty($where))
				{
					if($this->_debug == "true")
					{
						echo "SELECT DISTINCT $sql_str FROM $table";
					}
					$res = mysql_query("SELECT DISTINCT $sql_str FROM $table");
					if(!$res)
					{
						if($this->_debug == "true")
						{
							mysql_error();
							die;
						}
						else
						{
							die("Query database error.");
						}
					}
					
					while($result = mysql_fetch_array($res, MYSQL_ASSOC))
					{
						$row[] = $result;
					}
					return $row;
				}
				else
				{
					if($this->_debug == "true")
					{
						echo "SELECT DISTINCT $sql_str FROM $table WHERE $where";
					}
					$res = mysql_query("SELECT DISTINCT $sql_str FROM $table WHERE $where");
					if(!$res)
					{
						if($this->_debug == "true")
						{
							mysql_error();
							die;
						}
						else
						{
							die("Query database error.");
						}
					}
					
					while($result = mysql_fetch_array($res, MYSQL_ASSOC))
					{
						$row[] = $result;
					}
					return $row;
				}
			}
		}
		catch(Exception $ex)
		{
			if($this->_debug == "true")
			{
				echo $ex->getMessage();
				die;
			}
			else
			{
				echo "Can not query this table:$table";
				die;
			}
		}
	}
	
	public function customSql($sql)
	{
		if($this->_debug == "true")
		{
			echo $sql;
		}
		$res = mysql_query($sql);
		if(!$res)
		{
			if($this->_debug == "true")
			{
				mysql_error();
				die;
			}
			else
			{
				die("Query database error.");
			}
		}
		
		if(is_resource($res))
		{
			$row = array();
			while($result = mysql_fetch_array($res,MYSQL_ASSOC))
			{
				$row[] = $result;
			}
			return $row;
		}
		else
		{
			return $res;
		}
	}
	
	private function setInfo()
	{
//		$xmlObj = new lib_xml('./web.xml');
		$xmlObj = lib_xml::init();
		$this->_database = $xmlObj->getConfig('data/database');
		$this->_host = $xmlObj->getConfig('data/host');
		$this->_user = $xmlObj->getConfig('data/user');
		$this->_password = $xmlObj->getConfig('data/password');
		$this->_debug =  $xmlObj->getConfig('data/debug');
		$this->_prefix = $xmlObj->getConfig('data/prefix');
	}
}

?>